package com.ourhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ourhealth.mapper.MainMapper;
import com.ourhealth.pojo.Menu;
import com.ourhealth.pojo.User;
import com.ourhealth.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MainService.class)
@Transactional
public class MainServiceImpl implements MainService {
    @Autowired
    private MainMapper mainMapper;

    @Override
    public List<Menu> load(User user) {
        List<Menu> menus = mainMapper.load(user.getId());
        //一级目录
        List<Menu> firstMenus = new ArrayList<>();
        for (Menu menu : menus) {
            //判断是否为一级目录
            if (menu.getParentMenuId()==null||menu.getParentMenuId()==0){
                //是一级目录，将其加入数组
                firstMenus.add(menu);
            }
        }
//        for (Menu firstMenu : firstMenus) {
//            //二级目录
//            List<Menu> secondMenus = new ArrayList<>();
//            for (Menu menu : menus) {
//                //判断是否为二级目录
//                if (firstMenu.getId().equals(menu.getParentMenuId())){
//                    secondMenus.add(menu);
//                }
//            }
//            firstMenu.setChildren(secondMenus);
//        }
        recursionMenus(firstMenus, menus);

        return firstMenus;
    }

    public void recursionMenus(List<Menu> level,List<Menu> otherMenus){
        for (Menu thisMenu : level) {
            List<Menu> childMenu = new ArrayList<>();
            //判断剩余菜单链表里是否还有菜单，如果有则为下一级菜单
//            for (Menu menu : otherMenus) {
            for (int i = 0; i < otherMenus.size(); i++) {
                if (thisMenu==otherMenus.get(i)){
                    otherMenus.remove(otherMenus.get(i));
                    i--;
                }else {
                    if (otherMenus.get(i).getParentMenuId()!=null && otherMenus.get(i).getParentMenuId().equals(thisMenu.getId())){
                        childMenu.add(otherMenus.get(i));
                    }
                }
            }
            //如果子菜单长度不为0，则添加到父菜单
            if (childMenu!=null && childMenu.size()!=0){
                thisMenu.setChildren(childMenu);
            }

            if (otherMenus.size()==0){
                return;
            }else {
                recursionMenus(childMenu,otherMenus);
            }
        }
        return;
    }


    @Override
    public User findUserByUserName(String s) {
        return mainMapper.findUserByUserName(s);
    }

    @Override
    public void addMenu(Menu menu) {
        mainMapper.addMenu(menu);
        List<Integer> roleIds = mainMapper.findRoleIdByMenuId(menu.getParentMenuId());
        mainMapper.addRoleAndMenu(roleIds,menu.getId());
    }

    @Override
    public void remove(Integer id) {
        Integer num = mainMapper.findChildByMenuId(id);
        if (num==null || num==0){
            mainMapper.deleteRoleAndMenu(id);
            mainMapper.deleteMenu(id);
            return;
        }
        throw new RuntimeException();
    }
}
